package qlsl.androiddesign.http.service.commonservice;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import qlsl.androiddesign.constant.WhatConstant;
import qlsl.androiddesign.entity.otherentity.HomeBanner;
import qlsl.androiddesign.entity.otherentity.ShoopChildList;
import qlsl.androiddesign.handler.subhandler.HttpHandler;
import qlsl.androiddesign.http.HttpListener;
import qlsl.androiddesign.http.service.baseservice.BaseService;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.thread.HandlerThread;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.subview.commonview.TabView1;
import qlsl.androiddesign.view.subview.commonview.TabView2;
import qlsl.androiddesign.view.subview.commonview.TabView3;
import qlsl.androiddesign.view.subview.commonview.TabView5;

/**
 * 主页模块 <br/>
 * 
 */
public class HomeService extends BaseService {

	private static String className = getClassName(HomeService.class);

	/**
	 * 查询轮播图列表<br/>
	 * 
	 */
	public static void queryBannerList(final FunctionView<?> functionView, final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener, className, "queryBannerList");
		new HandlerThread(className, "queryBannerList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					int indexs = -1;
					if (functionView instanceof TabView1) {
						indexs = 1;
					} else if (functionView instanceof TabView2) {
						indexs = 2;
					} else if (functionView instanceof TabView3) {
						indexs = 3;
					} else if (functionView instanceof TabView5) {
						indexs = 5;
					}
					JSONObject jo = protocol.setMethod("getadvertisement").addParam("indexs", indexs).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					List<HomeBanner> list = JSONArray.parseArray(jo.getString("rows"), HomeBanner.class);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, list);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}

	/**
	 * 查询轮播图二级分类列表<br/>
	 * 
	 */
	public static void queryCategoryList(final String supplierid, final FunctionView<?> functionView,
			final HttpListener listener) {
		final HttpHandler handler = getHandler(functionView, listener, className, "queryCategoryList");
		new HandlerThread(className, "queryCategoryList") {
			public void run() {
				HttpProtocol protocol = new HttpProtocol();
				try {
					JSONObject jo = protocol.setMethod("getsubtiergoodstypelist").addParam("supplierid", 0).post();

					if (isDataInvalid(jo, handler, protocol)) {
						return;
					}

					List<ShoopChildList.RowsBean> list = JSONArray.parseArray(jo.getString("rows"),
							ShoopChildList.RowsBean.class);

					handler.sendMessage(protocol, WhatConstant.WHAT_NET_DATA_SUCCESS, list);
				} catch (Exception e) {
					e.printStackTrace();
					handler.sendMessage(protocol, WhatConstant.WHAT_EXCEPITON, e);
				}
			};
		}.start();
	}

}
